import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
import i18n from "@/i18n";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/oidc",
    component: () => import("@/views/login/oidc"),
    hidden: true,
  },
  {
    path: "/oidc_signout",
    component: () => import("@/views/login/oidc_signout"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: i18n.t("menus.dashboard"), icon: "dashboard" },
      },
    ],
  },
  {
    path: "/identity",
    component: Layout,
    redirect: "/client",
    meta: {
      title: "认证管理",
      icon: "iconfont icon-bank-account",
    },
    children: [
      {
        path: "scope",
        name: "scope",
        component: () => import("@/views/identity/resource/index"),
        meta: { title: "资源域管理", icon: "iconfont icon-bank-account" },
      },
      {
        path: "client",
        name: "Client",
        component: () => import("@/views/identity/client/index"),
        meta: { title: "客户端管理", icon: "iconfont icon-bank-account" },
      },
      {
        path: "clientedit",
        name: "ClientEdit",
        component: () => import("@/views/identity/client/edit"),
        meta: { title: "客户端编辑", icon: "iconfont icon-bank-account" },
        hidden: true,
      },
    ],
  },
  {
    path: "/permission",
    component: Layout,
    redirect: "/permission",
    meta: {
      title: "权限组管理",
      icon: "iconfont icon-fapiao",
    },
    children: [
      {
        path: "/permission/index",
        name: "PermissionIndex",
        component: () => import("@/views/permission/index"),
        meta: { title: "权限组管理", icon: "iconfont icon-fapiao" },
      },
    ],
  },
  {
    path: "/api-metadata",
    component: Layout,
    redirect: "/api-metadata",
    meta: {
      title: "接口管理",
      icon: "iconfont icon-fapiao",
    },
    children: [
      {
        path: "/apimetadata/index",
        name: "ApiMatedataIndex",
        component: () => import("@/views/apimetadata/index"),
        meta: { title: "接口管理", icon: "iconfont icon-fapiao" },
      },
    ],
  },
  {
    path: "/tenant",
    component: Layout,
    redirect: "/tenant",
    meta: {
      title: "租户管理",
      icon: "iconfont icon-fapiao",
    },
    children: [
      {
        path: "/tenant/index",
        name: "TenantIndex",
        component: () => import("@/views/tenant/index"),
        meta: { title: "租户管理", icon: "iconfont icon-fapiao" },
      },
    ],
  },
  {
    path: "/platformUser",
    component: Layout,
    redirect: "/platformUser",
    meta: {
      title: "平台用户管理",
      icon: "iconfont icon-xitongquanxian",
    },
    children: [
      {
        path: "/platformUser/index",
        component: () => import("@/views/platformUser/index"),
        name: "User",
        meta: {
          title: i18n.t("menus.user"),
          icon: "iconfont icon-yonghu",
        },
      },
      {
        path: "/platformUser/role",
        component: () => import("@/views/role/index"),
        name: "Role",
        meta: {
          title: i18n.t("menus.role"),
          icon: "iconfont icon-jiaoseguanli",
        },
      },
    ],
  },
  {
    path: "/enterpriseUser",
    component: Layout,
    redirect: "/enterpriseUser",
    meta: {
      title: "企业用户管理",
      icon: "iconfont icon-xitongquanxian",
    },
    children: [
      {
        path: "/enterpriseUser/index",
        component: () => import("@/views/enterpriseUser/index"),
        name: "User",
        meta: {
          title: "企业用户管理",
          icon: "iconfont icon-yonghu",
        },
      },
    ],
  },
  {
    path: "/setting",
    component: Layout,
    redirect: "/setting",
    meta: {
      title: "设置",
      icon: "iconfont icon-shezhi",
    },
    children: [],
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    mode: "history", // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
